package com.zsh.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.zsh.fragments.*;
import com.zsh.adapters.MainListAdapter;
import com.zsh.dialog.PubDialog;
import com.zsh.prototype.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity{
	private PubDialog mPubDialog;
	private FragmentTabHost mTabHost;
	private Class fragmentArray[] = { Main1Fragment.class, Main2Fragment.class, Main3Fragment.class, Main4Fragment.class, Main5Fragment.class };
	private int iconArray[] = { R.drawable.icon_home, R.drawable.icon_meassage,  R.drawable.icon_favorites, R.drawable.icon_selfinfo,
			R.drawable.icon_more };
	private String titleArray[] = { "首页", "爆料", "收藏", "个人", "更多" };
	
	private ImageButton slidemenuButton;

	MainListAdapter aAdpt;
	private AlertDialog alertDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		initSlidingMenu(savedInstanceState);
		setupTabView();
	}
	
	private void init() {
		mPubDialog = new PubDialog(this);
		mPubDialog.initTitleMenu();
	}

	private void createExitDialog(){
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.show();
		Window window = alertDialog.getWindow();
		window.setContentView(R.layout.exit_dialog);
		Button exit_btn = (Button) window.findViewById(R.id.exit_btn);
		Button cancel_btn = (Button) window.findViewById(R.id.cancel_btn);
		exit_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity.super.onBackPressed();
			}
		});
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});
	}


	private void initSlidingMenu(Bundle savedInstanceState) {
		slidemenuButton = (ImageButton)findViewById(R.id.imgbtn_top_left);
		slidemenuButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toggle();
			}
		});
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new LeftBottomFragment()).commit();		
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);		
	}

	@Override
	public void onBackPressed() {
		createExitDialog();
	}
	
	@Override
	protected void onDestroy() {
		if (alertDialog!=null) {
			alertDialog.dismiss();
		}
		super.onDestroy();
	}
	
	private void setupTabView() {
	     LayoutInflater.from(this);  
                  
        //ʵ��TabHost���󣬵õ�TabHost  
        mTabHost = (FragmentTabHost)findViewById(R.id.tabhost);  
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);   
          
        //�õ�fragment�ĸ���  
        int count = fragmentArray.length;     
                  
        for(int i = 0; i < count; i++){    
            //Ϊÿһ��Tab��ť����ͼ�ꡢ���ֺ�����  
            TabSpec tabSpec = mTabHost.newTabSpec(titleArray[i]).setIndicator(getTabItemView(i));  
            //��Tab��ť��ӽ�Tabѡ���  
            mTabHost.addTab(tabSpec, fragmentArray[i], null);  
            //����Tab��ť�ı���  
            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.home_btn_bg);  
        }  
	}

	private View getTabItemView(int index) {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		View view = layoutInflater.inflate(R.layout.view_idle_bottom_navi, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);
		imageView.setImageResource(iconArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.tv_icon);
		textView.setText(titleArray[index]);

		return view;
	}
	
	 
}
